#pragma once

#include <cstdint>
#include <linux/spi/spi.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/spi/spidev.h>
#include <string>
#include <mutex>

namespace HAL
{
    class SPI_Master
    {
        public:
            SPI_Master(const std::string &dev, uint32_t speed = 10000000, uint32_t mode = SPI_MODE_0, uint8_t bits = 8);
            SPI_Master(uint8_t port, uint8_t channel, uint32_t speed = 10000000, uint32_t mode = SPI_MODE_0, uint8_t bits = 8);
            ~SPI_Master();
            int WriteRead(uint8_t const *tx, uint8_t const *rx, size_t len);
        private:
            void SPI_Init(const std::string &dev, uint32_t speed, uint32_t mode, uint8_t bits);
        private:
            std::mutex mtx;
            std::string dev;
            int fd = -1;
            struct spi_ioc_transfer xfer;
    };
}
